// 判断用户登陆状态
const Authorization = localStorage.getItem('token')
window.user = {}
if (!Authorization) {
  location.href = './login.html'
}
$.ajax({
  url: '/my/userinfo'
}).then(res => {
  if (res.status === 1 && res.message ===  "身份认证失败！") {
    // 认证失败
    location.href = './login.html'
  } else {
    // 认证成功
    user.info = res.data
    render (user.info)
  }
})
// 默认首页
// $('.index')[0].click()
// 退出
$('.btn_quit').on('click', function() {
  layui.layer.confirm('是否退出复习', {icon: 3, title: '提示'}, val => {
    localStorage.removeItem('token')
    location.href = './login.html'
  })
})

// 渲染 界面
// 方便后面使用，定义为函数
// email: ""
// id: 1053
// nickname: ""
// user_pic: null
// username: "usernn5"
function render (data) {
  const {user_pic, username, nickname} = data
  let customName = nickname || username
  // 设置头像
  if (!user_pic) {
    $('.text_avat').text(username[0].toUpperCase())
  }else{
    $('.text_avat').hide()
    $('img.img_avat').attr('src', user_pic).show()
  }
  // 设置名称
  $('.username_text').text(customName)
}